<?php
/**
 * Created by PhpStorm.
 * User: 田家强
 * Date: 2017/3/27
 * Time: 10:49
 */

namespace Home\Controller;


use Think\Controller;

class ListPageController extends BaseContoller
{
    public function index()
    {
        $headConf = ['title'=>'维依尚品_商品展示','js'=>['list']];
        $this->assign('headConf',$headConf);
        $cid = I('get.cid');
        //1.处理面包屑[处理顶级]
        $fatherData = $this->getFather(m('category')->select(),$cid);
        //数组翻转 顶级在最上面
        $fatherData =  array_reverse($fatherData);
        //dd($fatherData);
        $this->assign('fatherData',$fatherData);
        //2.实现筛选
        //用递归找到当前分类[顶级]以及子集数据
        $cids = $this->getSon(m('category')->select(),$cid);
        $cids[]=$cid;
        //获取分类下面的所有子类
        $gids =m('goods')->where("cid in(".implode(',',$cids).")")->getField('gid',true);
        //dd($gids);
        if($gids) {
            //走到这里说明下面有商品
            //关联商品属性表和类型属性规格表
            $field = m ('goods_attr')->alias ('ga')
                ->join ('__TYPEATTR__ ta on ga.taid=ta.taid')
                ->where ("ga.gid in (" . implode (',', $gids) . ")")
                ->group ('ga.gtvalue')->select ();
            //dd($field);
            //定义空数组，对$field进行重组，taid一样的放一起
            $temp = [];
            foreach ($field as $k => $v) {
                $temp[ $v[ 'taid' ] ][] = $v;
            }
            $finalTemp = [];
            foreach ($temp as $k => $v) {
                $finalTemp[] = [
                    m ('typeattr')->where ("taid={$k}")
                        ->getField ('name'),
                    'value' => $v,
                ];
            }
            //dd($finalTemp);
            $this->assign ('finalTemp', $finalTemp);

            //准备地址栏的参数：?param=0_0_0_0
            $param = isset($_GET[ 'param' ]) ? explode ('_', $_GET[ 'param' ]) : array_fill (0, count ($finalTemp), 0);
//            dd($param);
            $this->assign ('param', $param);
            foreach ($param as $k => $v) {

                if ($v) {
                    //说明有筛选动作
                    $gtvalue = m ('goods_attr')->where ("gtid={$v}")->getField ('gtvalue');

                    //商品的属性名
                    $filterGids[] = m ('goods_attr')->where ("gtvalue='{$gtvalue}'")->getField ('gid', true);
                   }
            }
            //如果有filterGids，说你那个param参数中有不是0
            if ($filterGids) {
                $finalGids = $filterGids[ 0 ];
                foreach ($filterGids as $k => $v) {
                    $finalGids = array_intersect ($finalGids, $v);
                }
                //跟$gids求交集，foreach循环param中if判断里面，找出来的gid可能不在我们当前gids范围
                $finalGids = array_intersect ($finalGids, $gids);

            } else {
                //说明选的都是不限
                $finalGids = $gids;
            }
            //dd($filterGids);
        }else{
            //说明没有商品
            $finalGids = [];
        }
        //最后的商品id集合：$finalGids
        if($finalGids){
            //gid in (1,2)
            $data = m( 'goods' )->where( "gid in (".implode(',',$finalGids).")" )->select();
        }else{
            $data = [];
        }
//dd($data);


        //$data = m('goods')->select();
        $this->assign('data',$data);
        $this->display();

    }

    /**
     * 递归找子集
     */
    public function getSon($data,$cid)
    {
        static $temp = [];
        foreach($data as $k=>$v)
        {
            if($v['pid']==$cid)
            {
                $temp[] = $v['cid'];
                $this->getSon($data,$v['cid']);
            }
        }
        return $temp;
    }
    /**
     * 回去父级数据
     */
    public function getFather($data,$cid)
    {
        static $temp = [];
        foreach($data as $k=>$v)
        {
            if($v['cid']==$cid)
            {
                $temp[] = $v;
                $this->getFather($data,$v['pid']);
            }
        }
        return $temp;
    }
}